package cn.wen233.client.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * @author wenjie
 * @date 2022-03-21 16:44
 */
@Configuration
@EnableConfigurationProperties(SunglowAutoProperties.class)
public class SunglowAutoConfig {


    @Bean
    public PropertiesRunner propertiesRunner(SunglowAutoProperties properties) {
        return new PropertiesRunner(properties);
    }

    /**
     * 替换原有的SunglowServerProperties配置信息
     */
    public static class PropertiesRunner implements ApplicationRunner, Ordered {

        private static SunglowAutoProperties sunglowAutoProperties;

        public PropertiesRunner(SunglowAutoProperties sunglowAutoProperties) {
            PropertiesRunner.sunglowAutoProperties = sunglowAutoProperties;
        }

        @Override
        public void run(ApplicationArguments args) throws Exception {
            SunglowServerProperties.setNamespaceId(sunglowAutoProperties.getNamespaceId());
            SunglowServerProperties.setGroup(sunglowAutoProperties.getGroup());
            SunglowServerProperties.setIp(sunglowAutoProperties.getIp());
            SunglowServerProperties.setPort(sunglowAutoProperties.getPort());
        }

        @Override
        public int getOrder() {
            return 0;
        }
    }
}
